using System;
using System.Collections.Generic;

public class FlightPool : SingletonInit<FlightPool>
{
	private Dictionary<Type, List<BallFlight>> ballFlightPool = new Dictionary<Type, List<BallFlight>>();

	public override void Init()
	{
		base.Init();
		int num = 5;
		List<BallFlight> list = new List<BallFlight>();
		for (int i = 0; i < num; i++)
		{
			CatmullRomSplineBallFlight flightFromPool = GetFlightFromPool<CatmullRomSplineBallFlight>();
			int num2 = 50;
			flightFromPool.pool = this;
			for (int j = 0; j < num2; j++)
			{
				flightFromPool.AddControlPoint(default(FlightParams), j);
			}
			list.Add(flightFromPool);
		}
		foreach (BallFlight item in list)
		{
			item.ReturnToPool();
		}
	}

	public T GetFlightFromPool<T>() where T : BallFlight, new()
	{
		return GetFlightFromPool(typeof(T)) as T;
	}

	public BallFlight GetFlightFromPool(Type type)
	{
		if (!ballFlightPool.ContainsKey(type))
		{
			ballFlightPool.Add(type, new List<BallFlight>());
		}
		List<BallFlight> list = ballFlightPool[type];
		BallFlight ballFlight;
		if (list.Count > 0)
		{
			ballFlight = list[list.Count - 1];
			list.RemoveAt(list.Count - 1);
		}
		else
		{
			ballFlight = Activator.CreateInstance(type) as BallFlight;
		}
		ballFlight.pool = this;
		return ballFlight;
	}

	public void ReturnFlightToPool(BallFlight flight)
	{
		if (!ballFlightPool.ContainsKey(flight.GetType()))
		{
			ballFlightPool.Add(flight.GetType(), new List<BallFlight>());
		}
		List<BallFlight> list = ballFlightPool[flight.GetType()];
		list.Add(flight);
	}
}
